package os;

/**
 *
 * @author Paulius Dambrauskas
 */
class Interrupt extends Process {
    String interMsg = "OTHER";
    int govId = 0;
    int vmId = 0;
    ResourceElement e ;
    @Override
    public void runProcess() {
       this.changeStepCell(step);
       switch(step){
           case 1:{
               //changeStepCell(1);
               OS.askRes("Pertraukimo", selfId);
               incStep(1);
               break;
           }
           case 2:{
               //hangeStepCell(2);
               e = this.takeFromGivenResources("Pertraukimo");
               String[] interrupt = (String[]) e.value;
               //this.givenResources.remove("Pertraukimo"); //Reaik gal?
               String type = interrupt[0];
               if(type.equals("SI")){
                   switch(interrupt[1]){
                       case "5":{
                           interMsg = "LIGHT";
                           break;
                       }
                       case "4":{
                           interMsg = "HALT";
                           break;
                       }
                       case "3":{
                           interMsg = "READ";
                           break;
                       }
                       case "2":{
                           interMsg = "PRINT";
                           break;
                       }
                       case "1":{
                           interMsg = "START";
                           break;
                       }
                   }
                   vmId = Integer.parseInt(interrupt[2]);
                   incStep(1);
               } else {
                   Process pr = OS.processes.get(OS.prevProcess);
                   pr.setState(2);
                   OS.window.processor.getTI().setRegisterValue("10");
                   setStep(0);
               }
               
               
               break;
           }
           case 3:{
               //changeStepCell(3);
               govId = OS.processes.get(vmId).fatherId;
               incStep(1);
               break;
           }
           case 4:{
               //changeStepCell(4);
               //OS.createRes(selfId, "Pertraukimo pranešimo");
              
               ResourceElement el = new ResourceElement("Pertraukimo pranešimo", interMsg);
               el.setTarget(govId);
               OS.freeRes(el);
               setStep(1);
               break;
           }
       }
    }
    
}
